package com.android.signhelper.ui;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.signhelper.R;
import com.android.signhelper.base.BaseActivity;
import com.android.signhelper.bean.Student;
import com.android.signhelper.interfaces.TestContract;
import com.android.signhelper.presenter.TestPresenter;
import com.android.signhelper.util.JSONTool;

public class TestActivity extends BaseActivity<TestContract.View,TestPresenter> implements View.OnClickListener,TestContract.View {

    private TextView tvData;
    private EditText etUserName;
    private EditText etPassword;
    private Button btLogin;
    private Button btDetail;
    private Button btSign;
    private Student student;
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        initView();
        initData();
    }

    @Override
    protected TestContract.View createView() {
        return this;
    }

    @Override
    protected TestPresenter createPresenter() {
        return new TestPresenter();
    }

    public void initData()
    {
        student=getIntent().getParcelableExtra("student");
        etUserName.setText(student.getUserName());
        etPassword.setText(student.getPassword());
    }

    public void initView()
    {
        toolbar = findViewById(R.id.toolbar);
        tvData = findViewById(R.id.tv_data);
        btLogin = findViewById(R.id.bt_login);
        btDetail = findViewById(R.id.bt_detail);
        btSign = findViewById(R.id.bt_sign);
        etUserName = findViewById(R.id.et_userName);
        etPassword = findViewById(R.id.et_password);
        etUserName.setOnClickListener(this);
        etPassword.setOnClickListener(this);
        btLogin.setOnClickListener(this);
        btDetail.setOnClickListener(this);
        btSign.setOnClickListener(this);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.bt_login:

                getPresenter().login(student.getUserName(),student.getPassword());
                break;
            case R.id.bt_detail:
                getPresenter().detail(student.getToken(),student.getUserName());
                break;
            case R.id.bt_sign:
                getPresenter().sign(student.getToken(),student.getSignInfo());
                break;
        }
    }

    @Override
    public void showToast(String msg) {
    }
    @Override
    public void showData(String data) {
        tvData.setText(JSONTool.stringToJSON(data));
    }
}
